package com.toivan.mtcamera.mt_plugin

import android.content.Context
import android.util.Log
import androidx.annotation.NonNull
import com.toivan.mtcamera.mt_plugin.model.*
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import com.texeljoy.hteffect.HTEffect
import com.texeljoy.hteffect.model.*

/** MtPlugin */
class MtPlugin : FlutterPlugin, MethodCallHandler {

    private lateinit var channel: MethodChannel

    //用于初始化的Context
    private lateinit var applicationContext: Context

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "mt_plugin")
        applicationContext = flutterPluginBinding.applicationContext
        channel.setMethodCallHandler(this)
    }


    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

        Log.d("call.method:", call.method)

        when (call.method) {
            "getPlatformVersion" -> {
                result.success("Android ${android.os.Build.VERSION.RELEASE}")
            }

            MtAction.SET_RENDER_ENABLE.name -> {
                val isEnable: Boolean? = call.argument("enable")
                isEnable?.let {
                    HTEffect.shareInstance().setRenderEnable(isEnable)
                }
            }


            MtAction.SET_FACE_BEAUTY_ENABLE.name -> {
                val isEnable: Boolean? = call.argument("enable")
                isEnable?.let {
                    HTEffect.shareInstance().setRenderEnable(isEnable)
                }
            }


            MtAction.INIT_PATH.name -> {
                val paths = ConstraintsMap()
                paths.putString("maskPath", HTEffect.shareInstance().getARItemPathBy(1))
                paths.putString("stickerPath", HTEffect.shareInstance().getARItemPathBy(0))
                paths.putString("giftPath", HTEffect.shareInstance().getARItemPathBy(2))
                paths.putString("watermarkPath", HTEffect.shareInstance().getARItemPathBy(3))
                result.success(paths.toMap())
            }

            MtAction.INIT_SDK.name -> {
                Log.i("INIT_SDK:", "初始化触发")
                val key: String = call.argument("key") ?: return
                applicationContext.let {

                    //todo ---HTEffect--- 初始化SDK
                    HTEffect.shareInstance().initHTEffect(
                        it,"<YOUR_APP_ID>",
                        object : HTEffect.InitCallback {
                            override fun onInitSuccess() = Unit
                            override fun onInitFailure() = Unit
                        })
                }
            }

            MtAction.SET_WHITENESS_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautySkinWhitening.value, value)
                }
            }


            MtAction.SET_BLURRINESS_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautyClearSmoothing.value, value)
                }
            }

            MtAction.SET_PORTRAIT_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    HTEffect.shareInstance().setAISegEffect(value)
                }
            }

            MtAction.SET_ROSINESS_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautySkinRosiness.value, value)
                }
            }

            MtAction.SET_CLEAR_NESS_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautyImageSharpness.value, value)
                }
            }

            MtAction.SET_BRIGHTNESS_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautyImageBrightness.value, value)
                }
            }
            //去黑眼圈
            MtAction.SET_UNDEREYE_CIRCLES_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautyDarkCircleLessening.value, value)
                }
            }
            //去法令纹
            MtAction.SET_NASOLABIAL_FOLD_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setBeauty(HTBeautyEnum.HTBeautyNasolabialLessening.value, value)
                }
            }

            MtAction.SET_FACE_SHAPE_ENABLE.name -> {
                val value: Boolean? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance().setRenderEnable(value)
                }
            }

            MtAction.SET_EYE_ENLARGING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeEyeEnlarging.value, value);
                }
            }

            MtAction.SET_EYE_ROUNDING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeEyeRounding.value, value);
                }
            }

            MtAction.SET_CHEEK_V_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeCheekVShaping.value, value);
                }
            }

            MtAction.SET_FACE_SHORTENING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeCheekShortening.value, value);
                }
            }

            MtAction.SET_CHEEK_NARROWING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeCheekNarrowing.value, value);
                }
            }

            MtAction.SET_CHEEK_THINNING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeCheekThinning.value, value);
                }
            }

            MtAction.SET_CHIN_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeChinTrimming.value, value);
                }
            }

            MtAction.SET_FOREHEAD_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeForeheadTrimming.value, value);
                }
            }

            MtAction.SET_MOUTH_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeMouthTrimming.value, value);
                }
            }

            MtAction.SET_NOSE_THINNING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeNoseThinning.value, value);
                }
            }

            MtAction.SET_NOSE_ENLARGING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeNoseEnlarging.value, value);
                }
            }


            MtAction.SET_EYE_SPACING_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeEyeSpaceTrimming.value, value);
                }
            }

            MtAction.SET_EYE_CORNER_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeEyeCornerTrimming.value, value);
                }
            }

            MtAction.SET_DYNAMIC_STICKER_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    Log.d("应用贴纸:", value)
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemSticker.value, value)
                }
            }

            MtAction.SET_EXPRESSION_RECREATION_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                }
            }

            MtAction.SET_MASK_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemMask.value, value)
                }
            }

            MtAction.SET_GIFT_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemGift.value, value)
                }
            }

            MtAction.SET_WATERMARK_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemWatermark.value, value)
                }
            }

            MtAction.SET_WATER_NAME.name -> {

                val name: String? = call.argument("name")
                val x: Int? = call.argument("x")
                val y: Int? = call.argument("y")
                val ratio: Int? = call.argument("ratio")

                name?.let {
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemWatermark.value, name)
                }

            }

            MtAction.SET_PHILTRUM_TRIMMING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapePhiltrumTrimming.value, value);
                }
            }


            MtAction.SET_NOSE_APEX_LESSENING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeNoseApexLessening.value, value);
                }
            }

            MtAction.SET_TEMPLE_ENLARG_ING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeTempleEnlarging.value, value);
                }
            }

            MtAction.SET_FACE_LESSENING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeFaceLessening.value, value);
                }
            }

            MtAction.SET_HEAD_LESSENING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeHeadLessening.value, value);
                }
            }

            MtAction.SET_NOSE_ROOT_RNLARING.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeNoseRootEnlarging.value, value);
                }
            }

            MtAction.SET_JAW_BONE_THINNING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeJawboneThinning.value, value);
                }
            }

            MtAction.SET_CHEEK_BONE_THINNING.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeCheekboneThinning.value, value);
                }
            }

            MtAction.SET_MOUTH_SMILING_ENLARGING_VALUE.name -> {
                val value: Int? = call.argument("value")
                value?.let {
                    HTEffect.shareInstance()
                        .setReshape(HTReshapeEnum.HTReshapeMouthSmiling.value, value);
                }
            }

            MtAction.SET_ATMOSPHERE_ITEM_NAME.name -> {
                val value: String? = call.argument("name")
                value?.let {
                    HTEffect.shareInstance().setARItem(HTItemEnum.HTItemGift.value, value)
                }
            }

            MtAction.SET_BEAUTY_FILTER_NAME.name -> {
                val name: String? = call.argument("name")
                val value: Int? = call.argument("value")
                value?.let {
                    name?.let {
                        HTEffect.shareInstance().setFilter(HTFilterEnum.HTFilterBeauty.value, name)
                    }
                }
            }

            MtAction.SET_EFFECT_FILTER_TYPE.name -> {

                val name: String? = call.argument("name")

                val value: Int = call.argument("progress") ?: 0


                name?.let {
                    HTEffect.shareInstance().setFilter(HTFilterEnum.HTFilterEffect.value, name)
                }

            }

            MtAction.SET_FUNNY_FILTER_TYPE.name -> {
                val filterName: String? = call.argument("name")
                filterName?.let {
                    HTEffect.shareInstance().setFilter(HTFilterEnum.HTFilterFunny.value, filterName)
                }
            }
            MtAction.SET_BEAUTY_STYLE.name -> {
                val type: Int? = call.argument("type")
                type?.let {
                    HTEffect.shareInstance().setStyle(type)
                }
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}
